package top.malaoshi.controller.mgr;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.*;
import top.malaoshi.entity.Goods;
import top.malaoshi.model.JsonResult;
import top.malaoshi.service.GoodsSrvImpl;
import top.malaoshi.service.mgr.MgrGoodsSrvImpl;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/mgr")
@CrossOrigin
public class MgrGoodsCtrl {

    @Resource
    private MgrGoodsSrvImpl srv;

    @GetMapping("/goods/list")
    public JsonResult list(){
        JsonResult res=new JsonResult(0,"success");
        List<Goods> list=srv.list();
        if(list.size()==0) {
            res.setData(list);
        }else{
            List datas = new ArrayList(list.size());
            for (Goods goods : list) {
                try {
                    Map map = BeanUtils.describe(goods);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String createTime = sdf.format(goods.getCreateTime());
                    map.put("createTime", createTime);
                    map.remove("class");
                    datas.add(map);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
            res.setData(datas);
        }
        return res;
    }

}
